/*
* @(#)ListDmsDiscussionForumAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsValidation;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.core.DocumentValidateManager;
import com.dcivision.forum.web.ListForumGroupDetailForm;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;

/**
 * <p>Class Name:       ListDmsDiscussionForumAction.java    </p>
 * <p>Description:      The list action class for Forumn Group Detail.jsp</p>
 *
 *    @author           Zoe Shum
 *    @company          DCIVision Limited
 *    @creation date    15/10/2003
 *    @version          $Revision: 1.7.2.2 $
 */

public class ListDmsDiscussionForumAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.7.2.2 $";


  /**
   *  Constructor - Creates a new instance of ListDmsDiscussionForumAction and define the default listName.
   */
  public ListDmsDiscussionForumAction() {
    super();
    this.setListName("forumGroupDetailList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.forum.dao.ForumGroupDetailDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_PUBLIC_FOLDER);
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
  ListForumGroupDetailForm actionForm = (ListForumGroupDetailForm)form;
  if (!Utility.isEmpty(actionForm.getCurFunctionCode())) {
    this.setFunctionCode(actionForm.getCurFunctionCode());
  } else {
    this.setFunctionCode(null);
  }

  SessionContainer sessionContainer = this.getSessionContainer(request);
  Connection conn = this.getConnection(request);
  DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
  Integer documentID = TextUtility.parseIntegerObj(actionForm.getObjectID());
  DmsDocument document = docRetrievalManager.getDocument(documentID);
  if(DmsDocument.DOCUMENT_LINK.equals(document.getDocumentType())){
    document=docRetrievalManager.getRelationshipTargetDocument(document.getID(),DmsDocument.DOCUMENT_LINK);
    actionForm.setObjectID(document.getID().toString());
  }
  DocumentValidateManager docValidateManager = new DocumentValidateManager(sessionContainer, conn);

  DmsValidation validation = docValidateManager.validateOperateAction(document, false, "U");
  List lstIdMisRight = validation.getLstIdReject();
  List lstIdBeLocked = validation.getLstIdlocked();
  List lstIdBeDeleted = validation.getLstIdDeleted();
  List lstIdBeArchived = validation.getLstIdArchived();
  if (!lstIdMisRight.isEmpty() ||
      !lstIdBeDeleted.isEmpty() ||
      !lstIdBeArchived.isEmpty()){
    for (int i=0; i<lstIdMisRight.size(); i++) {
      this.addError(request,"errors.dms.no_permission",docRetrievalManager.getLocationPath((Integer)lstIdMisRight.get(i)));
    }
    for (int i=0; i<lstIdBeLocked.size(); i++) {
      this.addError(request,"errors.dms.been_checkout",docRetrievalManager.getLocationPath((Integer)lstIdBeLocked.get(i)));
    }
    for (int i=0; i<lstIdBeDeleted.size(); i++) {
      this.addError(request,"errors.dms.been_deleted",docRetrievalManager.getLocationPath((Integer)lstIdBeDeleted.get(i)));
    }
    for (int i=0; i<lstIdBeArchived.size(); i++) {
      this.addError(request,"errors.dms.been_archived",docRetrievalManager.getLocationPath((Integer)lstIdBeArchived.get(i)));
    }
    if (Utility.isEmpty(actionForm.getReturnTo())){
      return mapping.findForward("Home");
    } else {
      ActionForward actionForward = new ActionForward();
      actionForward.setPath("/dms/" + actionForm.getReturnTo());
      return actionForward;
    }
  }
  
  return super.execute(mapping, form, request, response);
}

}
